package T100;

/**
 * @Author cheng.song
 * @create 2022/1/27 16:44
 */
public class 轮转数组189 {

    public static void main(String[] args) {

    }

    // 反转解法
    public void rotate1(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }


    // 取余解法
    public void rotate(int[] nums, int k) {
        int temp[] = new int[nums.length];
        for(int i=0;i<nums.length;i++){
            temp[i]=nums[i];
        }
        for(int i=0;i<nums.length;i++){
            nums[(i+k)%nums.length]=temp[i];
        }
    }

    public void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start += 1;
            end -= 1;
        }
    }
}
